package top.maof.book.spider.develop.book.qidian;


import lombok.Data;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.util.Date;

/**
 * 起点中文网
 */
@Data
@TargetUrl("https://book.qidian.com/info/\\d+")
@HelpUrl(value = {"https://www.qidian.com/all/[\\w\\?=/&]*", "https://www.qidian.com/mm/all/[\\w\\?=/&]*", "https://www.qidian.com/xuanhuan",
        "https://www.qidian.com/mm/rank/[\\w\\?=/&]*", "https://www.qidian.com/rank/[\\w\\?=/&]*", "https://www.qidian.com/", "https://www.qidian.com/finish/[\\w\\?=/&]*",
        "https://www.qidian.com/free/[\\w\\?=/&]*", "https://www.qidian.com/mm/finish/[\\w\\?=/&]*",
        "https://www.qidian.com/mm/[\\w\\?=/&]*", "https://www.qidian.com/\\w*", "https://www.qdmm.com/\\w*"})
public class QiDian implements AfterExtractor, Split {

    public static final String CHARSET = "utf-8";

    @ExtractBy("//div[@class=book-info]/h1/em/text()")
    private String name;

    @ExtractBy("//div[@class=book-info]/h1/span/a/text()")
    private String author;

    @ExtractBy("//div[@class=book-img]/a/img/@src")
    private String img;

    @ExtractBy("//div[@class=crumbs-nav]/span/a[2]/text()")
    private String classify;

    @ExtractBy("//div[@class=book-info]/p[1]/span[1]/text()")
    private String state;

    /**
     * 书收录时间
     */
    private Date insertDate = new Date();

    /**
     * 最新章节更新时间
     */
    private Date updateDate;

    /**
     * 书源站点名字
     */
    private String sourceSite;

    /**
     * 书源站点地址
     */
    private String sourceUrl;

    /**
     * 书开始连载时间
     */
    private Date startDate;

    @ExtractBy("//div[@class=book-intro]/p[1]/text()")
    private String synopsis;


    @Override
    public Book getBook() {
        return new Book(null, name, author, img, classify, state,
                insertDate, updateDate, sourceSite, sourceUrl, startDate, synopsis);
    }

    @Override
    public Contents getContents() {
        return null;
    }

    @Override
    public void afterProcess(Page page) {
        this.sourceSite = "起点中文网";
        this.sourceUrl = page.getUrl().toString();
        this.img = "https:" + this.img;
        this.name = this.name.trim();
        this.author = this.author.trim();
    }
}
